﻿using System;
using System.Windows.Data;
using System.IO;
using System.Windows.Media.Imaging;
using System.Globalization;

namespace OverlakeApp.Converters
{
    /// <summary>
    /// A converter so that a binary (byte[]) image can be bound to an ImageSource.
    /// </summary>
    public class BinaryImageConverter : IValueConverter
    {
        /// <summary>
        /// Convert from a byte[] to an ImageSource
        /// </summary>
        /// <param name="value">The byte array to convert.</param>
        /// <param name="targetType">The type of the target.</param>
        /// <param name="parameter">Not used.</param>
        /// <param name="culture">Not used.</param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || !(value is byte[]))
                return null;

            var bitImg = new BitmapImage();
            //bitImg.ini
            var ms = new MemoryStream((byte[])value);
#if PC
            bitImg.StreamSource = ms;
#else
            bitImg.SetSource(ms);
#endif       
            return bitImg;
        }

        /// <summary>
        /// Not supported.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
